﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

using ERPStore.Extensions;

using ERPStore.Web.Routing;
using ERPStore.Web.Extensions;

using Microsoft.Practices.Unity;

using CloudSoft.Extensions;

namespace ERPStore.Payment.Direct
{
	public class PluginInit : ERPStore.Extensibility.PluginInit
	{
		public PluginInit()
		{
			Container = GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
		}

		Microsoft.Practices.Unity.IUnityContainer Container { get; set; }

		public override void AddServices()
		{
			var checkPaymentSettings = new Models.Settings();
			checkPaymentSettings.BindFromConfiguration(ERPStore.Payment.Direct.Configuration.ConfigurationSettings.GetAppSettings("erpStore/paymentByCheckSettings"));
			Container.RegisterInstance("paymentByCheckSettings", checkPaymentSettings, new ContainerControlledLifetimeManager());

			var deferredPaymentSettings = new Models.Settings();
			deferredPaymentSettings.BindFromConfiguration(ERPStore.Payment.Direct.Configuration.ConfigurationSettings.GetAppSettings("erpStore/deferredPaymentSettings"));
			Container.RegisterInstance("deferredPaymentSettings", deferredPaymentSettings, new ContainerControlledLifetimeManager());

			var wireTransferPaymentSettings = new Models.Settings();
			wireTransferPaymentSettings.BindFromConfiguration(ERPStore.Payment.Direct.Configuration.ConfigurationSettings.GetAppSettings("erpStore/paymentByWireTransferSettings"));
			Container.RegisterInstance("paymentByWireTransferSettings", wireTransferPaymentSettings, new ContainerControlledLifetimeManager());

			var sandboxSettings = new Models.SandboxSettings();
			sandboxSettings.BindFromConfiguration(ERPStore.Payment.Direct.Configuration.ConfigurationSettings.GetAppSettings("erpStore/sandboxPaymentSettings"));
			Container.RegisterInstance("sandboxPaymentSettings", sandboxSettings);

			// Enregistrement des modes de reglement par defaut
			Container.RegisterType<ERPStore.Services.IPaymentService, Services.PaymentByCheckService>("check", new ContainerControlledLifetimeManager());
			Container.RegisterType<ERPStore.Services.IPaymentService, Services.PaymentByWireTransferService>("wiretransfer", new ContainerControlledLifetimeManager());
			Container.RegisterType<ERPStore.Services.IPaymentService, Services.PaymentDeferredService>("deferredpayment", new ContainerControlledLifetimeManager());
			Container.RegisterType<ERPStore.Services.IPaymentService, Services.SandBoxPaymentService>("sandbox", new ContainerControlledLifetimeManager());

			Container.RegisterType<ERPStore.Services.ICouponProvider, Services.SandboxCouponProvider>("sandboxcouponprovider", new ContainerControlledLifetimeManager());
		}

		public override void Load()
		{
			var sandboxPayment = Container.Resolve<Services.SandBoxPaymentService>("sandbox");
			if (sandboxPayment != null 
				&& !sandboxPayment.CouponCode.IsNullOrTrimmedEmpty())
			{
				ERPStore.GlobalConfiguration.Configuration.Settings.Payment.AllowedPaymentList.Add("sandbox");
			}
		}

		public override void RegisterRoutes()
		{
			RegisterWireTransferRoutes(System.Web.Routing.RouteTable.Routes);
			RegisterDeferredPaymentRoutes(System.Web.Routing.RouteTable.Routes);
			RegisterCheckPaymentRoutes(System.Web.Routing.RouteTable.Routes);
			RegisterSandboxPaymentRoutes(System.Web.Routing.RouteTable.Routes);
		}

		protected virtual void RegisterWireTransferRoutes(System.Web.Routing.RouteCollection routes)
		{
			var namespaces = new string[] { "ERPStore.Payment.Direct.Controllers" };

			routes.MapERPStoreRoute(
				"WiretransferPaymentConfirmation"
				, "commander/virement/confirmation"
				, new { controller = "WireTransferPayment", action = "PaymentConfirmation" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				"WiretransferPaymentFinalized"
				, "commander/virement/merci"
				, new { controller = "WireTransferPayment", action = "Finalized" }
				, namespaces
			);

		}

		protected virtual void RegisterDeferredPaymentRoutes(System.Web.Routing.RouteCollection routes)
		{
			var namespaces = new string[] { "ERPStore.Payments.Direct.Controllers" };

			routes.MapERPStoreRoute(
				"DeferredPaymentConfirmation"
				, "commander/differe/confirmation"
				, new { controller = "DeferredPayment", action = "PaymentConfirmation" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				"DeferredPaymentFinalized"
				, "commander/differe/merci"
				, new { controller = "DeferredPayment", action = "Finalized" }
				, namespaces
			);
		}

		protected virtual void RegisterCheckPaymentRoutes(System.Web.Routing.RouteCollection routes)
		{
			var namespaces = new string[] { "ERPStore.Payments.Direct.Controllers" };

			routes.MapERPStoreRoute(
				"CheckPaymentConfirmation"
				, "commander/cheque/confirmation"
				, new { controller = "CheckPayment", action = "PaymentConfirmation" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				"CheckPaymentFinalized"
				, "commander/cheque/merci"
				, new { controller = "CheckPayment", action = "Finalized" }
				, namespaces
			);
		}

		protected virtual void RegisterSandboxPaymentRoutes(System.Web.Routing.RouteCollection routes)
		{
			var namespaces = new string[] { "ERPStore.Payments.Direct.Controllers" };

			routes.MapERPStoreRoute(
				"SandboxPaymentConfirmation"
				, "commander/sandbox/confirmation"
				, new { controller = "SandboxPayment", action = "PaymentConfirmation" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				"SandboxPaymentFinalized"
				, "commander/sandbox/merci"
				, new { controller = "SandboxPayment", action = "Finalized" }
				, namespaces
			);
		}
	}
}
